package com.lc1993929.shucao.util;

import com.lc1993929.shucao.constants.Constants;
import com.lc1993929.shucao.entity.PosInfo;
import com.lc1993929.shucao.enums.Direct;
import com.lc1993929.shucao.enums.MapTypeEnum;
import com.lc1993929.shucao.map.Coord;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class MapUtil {


    public static Coord findCoord(List<List<PosInfo>> map, String playerName) {
        for (int y = 0; y < map.size(); y++) {
            List<PosInfo> posInfos = map.get(y);
            for (int x = 0; x < posInfos.size(); x++) {
                PosInfo posInfo = posInfos.get(x);
                if ((MapTypeEnum.shu1.getValue().equals(playerName) && posInfo.getShu1())
                        || (MapTypeEnum.shu2.getValue().equals(playerName) && posInfo.getShu2())
                        || (MapTypeEnum.shu3.getValue().equals(playerName) && posInfo.getShu3())
                        || (MapTypeEnum.shu4.getValue().equals(playerName) && posInfo.getShu4())
                        || (MapTypeEnum.cao1.getValue().equals(playerName) && posInfo.getCao1())
                        || (MapTypeEnum.cao2.getValue().equals(playerName) && posInfo.getCao2())
                        || (MapTypeEnum.cao3.getValue().equals(playerName) && posInfo.getCao3())
                        || (MapTypeEnum.cao4.getValue().equals(playerName) && posInfo.getCao4())
                        || (MapTypeEnum.target.getValue().equals(playerName) && posInfo.getTarget())
                ) {
                    return new Coord(x, y);
                }
            }

        }
        return null;
    }

    public static PosInfo getPosInfoByCoord(Coord coord) {
        // 是否在地图中
        if (coord.getX() < 0 || coord.getX() >= Constants.playerMap.getColLen() || coord.getY() < 0 || coord.getY() >= Constants.playerMap.getRowLen()) {
            return null;
        }

        List<PosInfo> posInfos = Constants.playerMap.getMap().get(coord.getY());
        if (CollectionUtils.isEmpty(posInfos)) {
            return null;
        }
        return posInfos.get(coord.getX());
    }

    /**
     * 获取一个随机的方向
     */
    public static Direct getRandomDirection() {
        Integer random = Util.getRandomInt(4);
        Direct direction = Direct.STAY;
        if (random == 0) {
            direction = Direct.LEFT;
        } else if (random == 1) {
            direction = Direct.RIGHT;
        } else if (random == 2) {
            direction = Direct.UP;
        } else if (random == 3) {
            direction = Direct.DOWN;
        }
        return direction;

    }

    public static void mapPrint(List<List<PosInfo>> map) {
        map.forEach(posInfos -> {
            System.out.print("[\t");
            posInfos.forEach(posInfo -> {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("[");
                List<String> list = new ArrayList<>();
                if (posInfo.getTarget()) {
                    list.add("$");
                }
                if (posInfo.getBlock()) {
                    list.add("0");
                } else if (posInfo.getUnknown()) {
                    list.add("*");
                } else {
                    if (posInfo.getCao1()) {
                        list.add("2");
                    }
                    if (posInfo.getCao2()) {
                        list.add("3");
                    }
                    if (posInfo.getCao3()) {
                        list.add("4");
                    }
                    if (posInfo.getCao4()) {
                        list.add("5");
                    }
                    if (posInfo.getShu1()) {
                        list.add("6");
                    }
                    if (posInfo.getShu2()) {
                        list.add("7");
                    }
                    if (posInfo.getShu3()) {
                        list.add("8");
                    }
                    if (posInfo.getShu4()) {
                        list.add("9");
                    }
                    if (list.isEmpty() && posInfo.getRoad()) {
                        list.add("#");
                    }
                }
                stringBuilder.append(String.join(",", list));
                stringBuilder.append("]\t\t");
                System.out.print(stringBuilder.toString());
            });
            System.out.println("]\t");
        });
    }

}
